#include "Game Engine\Header\RigidBody.h"

RigidBody::RigidBody()
{
	mCharacter		= NULL;
	mGhostObject	= NULL;
}

RigidBody::~RigidBody()
{
}

void RigidBody::Init( ID3D10Device* device, btDynamicsWorld* world, std::wstring meshFile,
	ID3D10Effect* fx, ID3D10ShaderResourceView* cubeMap, float mass,
	Physics::Shape shape, bool useShadows )
{
	Body::Init( device, world, meshFile, fx, cubeMap, mass, shape, useShadows );

	mBody = CreateRigidBodyGeneric( mDynamicsWorld, mMesh, mMass, shape );
}

void RigidBody::Init( ID3D10Device* device, btDynamicsWorld* world, std::vector<std::wstring> meshFiles,
	ID3D10Effect* fx, ID3D10ShaderResourceView* cubeMap, float mass,
	Physics::Shape shape, bool useShadows )
{
	Body::Init( device, world, meshFiles, fx, cubeMap, mass, shape, useShadows );

	btTransform trans;

	trans.setIdentity();
	trans.setOrigin( BTVector3( mPosition ) );
	mBody = CreateRigidBody( mDynamicsWorld, mMass, trans, mCompound );

	btTransform loadTrans;
	loadTrans.setIdentity();
	loadTrans.setOrigin( BTVector3( mPosition ) );
	mBody->activate();
	mBody->setCenterOfMassTransform( loadTrans );
	mBody->setLinearVelocity( btVector3( 0, 0, 0 ) );
	mBody->setAngularVelocity( btVector3( 0, 0, 0 ) );
	mBody->setVelocity( btVector3( 0.0f, 0.0f, 0.0f ) );

	if( mMass == 0.0f )
		mBody->setCollisionFlags(mBody->getCollisionFlags()  | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
}

void RigidBody::Strafe( float d )
{
	mVelocity.x += d;
}

void RigidBody::Walk( float d )
{
	mVelocity.z += d;
}

void RigidBody::Move( float dt )
{
}

void RigidBody::Stop()
{
}

D3DXVECTOR3& RigidBody::GetPosition()
{
	mPosition = BT2DX_VECTOR3( mBody->getWorldTransform().getOrigin() );

	return mPosition;
}

D3DXVECTOR3& RigidBody::GetVelocity()
{
	mVelocity = BT2DX_VECTOR3( mBody->getVelocity() );

	return mVelocity;
}

void RigidBody::SetPosition( D3DXVECTOR3 pos )
{
	mPosition = pos;

	btTransform posTrans;
	posTrans.setIdentity();
	posTrans.setOrigin( BTVector3( mPosition ) );
	posTrans.setRotation( btQuaternion( mYaw, mPitch, mRoll ) );
	mBody->setCenterOfMassTransform( posTrans );
}

void RigidBody::SetVelocity( D3DXVECTOR3 vel )
{
	mVelocity = vel;

	mBody->setConveyorVelocity( BTVector3( mVelocity ) );
	mBody->setVelocity( BTVector3( mVelocity ) );
}

void RigidBody::Draw( D3DXMATRIX& lightViewProj )
{
	// Get rid of this line if you want to make the object go to sleep.
	mBody->activate();

	if( mMass == 0.0f )
	{
		btTransform trans = mBody->getWorldTransform();
		mWorld = BT2DX_MATRIX( trans );
	}
	else
	{
		float velLength = D3DXVec3Length( &BT2DX_VECTOR3( mBody->getLinearVelocity() ) );
		if( mBody->getGravity() != btVector3( 0.0f, 0.0f, 0.0f ) )
			mBody->setDamping( mBody->getFriction() / 2.0f, mBody->getFriction() / 2.0f );

		btMotionState* ms = mBody->getMotionState();
		if(ms != NULL)
			mWorld = BT2DX_MATRIX( *ms );
	}

	if( mBody->getCollisionShape()->isCompound() )
	{
		btCompoundShape* compound = ( btCompoundShape* )mBody->getCollisionShape();
		for( int i = 0; i < compound->getNumChildShapes(); i++ )
		{
			btTransform child = compound->getChildTransform(i);
			btTransform ms = mBody->getWorldTransform() * child;
			mMeshVector[i]->Draw( BT2DX_MATRIX( ms ), lightViewProj );
		}
	}
	else
		mMesh->Draw( mWorld, lightViewProj );
}